package com.jiang.mall.mallorder.controller;

import com.jiang.common.utils.PageUtils;
import com.jiang.common.utils.R;
import com.jiang.mall.mallorder.entity.OrderEntity;
import com.jiang.mall.mallorder.entity.OrderReturnReasonEntity;
import com.jiang.mall.mallorder.service.OrderService;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.UUID;


/**
 * 订单
 *
 * @author zhangsan
 * @email 2469939264@qq.com
 * @date 2021-03-23 17:06:45
 */
@RestController
@RequestMapping("order/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 订单列表
     */
    @PostMapping("/listWithItem")
    public R listWithItem(@RequestBody Map<String, Object> params){
        PageUtils page = orderService.queryPageWithItem(params);
        return R.ok().put("page", page);
    }


    @PostMapping("/status/{orderSn}")
    public R getOrderStatus(@PathVariable String orderSn) {
        OrderEntity orderEntity = orderService.getOrderByOrderSn(orderSn);
        return R.ok().put("data", orderEntity);
    }

    @ResponseBody
    @GetMapping("/sendMQ")
    public String sendMQ(@RequestParam(value = "num",defaultValue = "10")Integer num) {
        OrderReturnReasonEntity orderReturnReasonEntity = new OrderReturnReasonEntity();
        orderReturnReasonEntity.setCreateTime(new Date());
        orderReturnReasonEntity.setId(1L);
        for(int i = 0; i < num; i ++) {
            if(i%2 == 0) {
                orderReturnReasonEntity.setName("hello=" + i);
                rabbitTemplate.convertAndSend("hello-exhcange","hello",orderReturnReasonEntity,new CorrelationData(UUID.randomUUID().toString()));
            }else {
                OrderEntity orderEntity = new OrderEntity();
                orderEntity.setReceiverName("小明=" + i);
//                rabbitTemplate.convertAndSend("hello-exhcange","hello",orderEntity);
                // 错误的路由键，模拟消息发送不到队列的错误回调
                rabbitTemplate.convertAndSend("hello-exhcange","hello-error",orderEntity,new CorrelationData(UUID.randomUUID().toString()));
            }

        }
        return "ok";
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("order:order:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = orderService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("order:order:info")
    public R info(@PathVariable("id") Long id) {
        OrderEntity order = orderService.getById(id);

        return R.ok().put("order", order);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("order:order:save")
    public R save(@RequestBody OrderEntity order) {
        orderService.save(order);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("order:order:update")
    public R update(@RequestBody OrderEntity order) {
        orderService.updateById(order);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("order:order:delete")
    public R delete(@RequestBody Long[] ids) {
        orderService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
